package com.ruoyi.orders.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.items.domain.TbOrderItems;
import com.ruoyi.items.mapper.TbOrderItemsMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.orders.mapper.TbOrdersMapper;
import com.ruoyi.orders.domain.TbOrders;
import com.ruoyi.orders.service.ITbOrdersService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单管理Service业务层处理
 * 
 * @author txt
 * @date 2025-02-05
 */
@Service
public class TbOrdersServiceImpl implements ITbOrdersService 
{
    @Autowired
    private TbOrdersMapper tbOrdersMapper;
    @Autowired
    private TbOrderItemsMapper tbOrderItemsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询订单管理
     * 
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public TbOrders selectTbOrdersByOrderId(Long orderId)
    {
        return tbOrdersMapper.selectTbOrdersByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param tbOrders 订单管理
     * @return 订单管理
     */
    @Override
    @Transactional(readOnly = true)
    public List<TbOrders> selectTbOrdersList(TbOrders tbOrders)
    {
        List<TbOrders> tbOrders1 = tbOrdersMapper.selectTbOrdersList(tbOrders);

        for (TbOrders order : tbOrders1) {
            List<TbOrderItems> detailInfo = tbOrderItemsMapper.getDetailInfo(order.getOrderId());
            order.setTbOrderItems(detailInfo);
        }
        return tbOrders1;
    }

    /**
     * 新增订单管理
     * 
     * @param tbOrders 订单管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbOrders(TbOrders tbOrders)
    {

        int result = tbOrdersMapper.insertTbOrders(tbOrders);
        if (result > 0 && tbOrders.getTbOrderItems() != null) {
            // 如果订单详情存在，循环插入每个订单项
            for (TbOrderItems orderItem : tbOrders.getTbOrderItems()) {
                orderItem.setOrderId(tbOrders.getOrderId()); // 设置订单ID到每个订单项
                tbOrderItemsMapper.insertTbOrderItems(orderItem); // 插入订单项
            }
        }
        Long userId = SecurityUtils.getLoginUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        BigDecimal balance = sysUser.getBalance();
        BigDecimal totalPrice = tbOrders.getTotalPrice();
        if (balance.compareTo(totalPrice) < 0) {
            // 如果余额不足，抛出异常
            throw new ServiceException("余额不足",500);
        }
        BigDecimal newBalance = balance.subtract(totalPrice);
        sysUser.setBalance(newBalance);

        sysUserMapper.updateUser(sysUser);
        return result;
    }

    /**
     * 修改订单管理
     * 
     * @param tbOrders 订单管理
     * @return 结果
     */
    @Override
    public int updateTbOrders(TbOrders tbOrders)
    {
        return tbOrdersMapper.updateTbOrders(tbOrders);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTbOrdersByOrderIds(Long[] orderIds)
    {
        return tbOrdersMapper.deleteTbOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTbOrdersByOrderId(Long orderId)
    {
        return tbOrdersMapper.deleteTbOrdersByOrderId(orderId);
    }
}
